Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
Note: Could you optimize your algorithm to use only O(k) extra space?

题目大意:给定k,返回杨辉三角的第k行,空间复杂度要求O(K)

题目难度:Easy

import java.util.*;

/**
 * Created by gzdaijie on 16/6/11
 */
public class Solution {
    public List<Integer> getRow(int rowIndex) {
        Queue<Integer> result = new LinkedList<>();
        result.offer(1);

        for (int i = 0; i < rowIndex; i++) {
            int size = result.size();
            int pre = 0;
            while (size-- > 0) {
                int top = result.poll();
                result.offer(pre + top);
                pre = top;
            }
            result.add(1);
        }
        return (List<Integer>)result;
    }
}
gzdaijie            updated 2016-06-11 12:39:44

results matching ""

    No results matching ""